package httpHandle

import (
	"github.com/gin-gonic/gin"
	"github.com/wzh200x/webServer/constants"
	"github.com/wzh200x/webServer/httpHandle/service"
	"github.com/wzh200x/webServer/middleware/auth"
	"github.com/wzh200x/webServer/middleware/response"
)

// 使用service包中定义的请求和响应结构
type UseCdKeyRequest service.UseCdKeyRequest
type CdKeyResponse service.CdKeyResponse
type CdKeyGroupResponse service.CdKeyGroupResponse
type UseCdKeyResponse service.UseCdKeyResponse

// HandleUseCdKey 处理使用CDKey请求
func HandleUseCdKey(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)

	// 从上下文中获取用户信息
	_, username := auth.GetCurrentUser(c)

	// 解析请求参数
	var req UseCdKeyRequest
	if bindErr := c.ShouldBind(&req); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}

	// 转换为service层的请求结构
	serviceReq := service.UseCdKeyRequest{
		AvatarId: req.AvatarId,
		Code:     req.Code,
	}

	// 调用service层使用CDKey
	response, errCode, errMsg := service.UseCdKey(username, serviceReq)
	if errCode > 0 {
		err(errCode, errMsg)
		return
	}

	// 返回CDKey使用结果
	success(response)
}
